package com.ifcar.app.rss;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.ifcar.app.util.HttpUtils;
import com.ifcar.app.util.NeteaseHttpClient;
import com.ifcar.app.util.Tools;
import com.jakewharton.DiskLruCache;

public class GetComments {

	public static final String PRCOUNT_TAG = "prcount";
	public static final String HOTPOSTS_TAG = "hotPosts";
	public static final String NEWPOSTS_TAG = "newPosts";
	public static final String CODE_TAG = "code";
	public static final String PTCOUNT_TAG = "ptcount";
	
	public static final String ERRMSG_TAG = "errMsg";
	
	public static final String VOTE_TAG = "v";
	public static final String USERNAME_TAG = "u";
	public static final String TIME_TAG = "t";
	public static final String B_TAG = "b";
	public static final String P_TAG = "p";
	public static final String BOARDID_TAG = "bi";
	public static final String PI_TAG = "pi";
	public static final String IP_TAG = "ip";
	public static final String F_TAG = "f";
	public static final String L_TAG = "l";
	
	public static final String NON_TAG = "NON";
	
	public static final String BOA_TAG = "board";
	public static final String THREADID_TAG = "threadid";
	public static final String QUOTE_TAG = "quote";
	public static final String BODY_TAG = "body";
	public static final String USERID_TAG = "userid";
	public static final String NICKNAME_TAG = "nickname";
	public static final String HIDDENAME_TAG = "hidename";
	
	public static final String POSTID_TAG = "postid"; 
	public static final String REPLYCOUNT_TAG = "replyCount";
	public static final String TIES_TAG = "ties";
	public static final String TITLE_TAG = "title";
	public static final String DOCID_TAG = "docid";
	public static final String REPLYBOARD_TAG = "replyBoard";
	public static final String FLOOR_TAG = "floor";
	public static final String AUTHOR_TAG = "author";
	public static final String TIE_TAG = "tie";
	public static final String PTIME_TAG = "ptime";
	
	public static final String BASE_COMMENTS_URL = "http://comment.api.163.com/";
	public static final String HOT_COMMENTS_URL = BASE_COMMENTS_URL + "api/json/post/list/new/hot/";//"http://comment.api.163.com/api/json/post/list/hot/";	
	public static final String LATEST_COMMENTS_URL = BASE_COMMENTS_URL + "api/json/post/list/new/normal/";//"http://comment.api.163.com/api/json/post/list/normal/";			
	public static final String POST_COMMENTS_URL = "http://comment.api.163.com/api/jsonp/post/insert";
	public static final String ONE_COMMENT_URL = "http://comment.api.163.com/api/json/post/load/";
	public static final String TOP_COMMENTS_URL = "http://c.3g.163.com/nc/comment/ties/";
	public static final String Rank_Comments_Url = "http://c.3g.163.com/nc/comment/rank/";
//	public static final String Up_Vote_Url = "http://comment.api.163.com/api/reply/upvote/";
	public static final String Up_Vote_Url = "http://comment.news.163.com/reply/upvote/";
	public static final String TRANSFER_COMMENTTOMICROBLOG_URL="http://comment.api.163.com/api/jsonp/tinneyblog/transfer";
	
	public static final String IMAGE_COMMET_URL = "http://c.3g.163.com/photo/api/set/0096/";
	public static final String IMAGE_COMMENT_NUM_URL = "http://comment.news.163.com/api/json/thread/total/";
	
	public static HashMap<String, Object> getHotComments(
			String boardId, String docId, int start, int end, int hidenCondition, int hiddenStart, int hiddenEnd,Context mContext) {
		HashMap<String, Object> resultsMap = new HashMap<String, Object>();
		List<List<HashMap<String, Object>>> list = new ArrayList<List<HashMap<String, Object>>>();
		List<HashMap<String, Object>> floorList = null;

		JSONObject jobj = null;
		JSONObject jobj2 = null;
		JSONObject jobj3 = null;
		JSONArray joArray = null;
		HashMap<String, Object> map = null;

		String url = HOT_COMMENTS_URL + boardId + "/" + 
		    docId + "/" + start + "/" + end + "/" + hidenCondition + "/" + hiddenStart + "/" + hiddenEnd;
		String results = HttpUtils.getResponseFromGetUrl(url, 30000,mContext);
		
		if (results != null && !results.equals("")) {

			try {
				jobj = new JSONObject(results);
				if(jobj != null) {
					
					resultsMap.put(CODE_TAG, jobj.getString(CODE_TAG));
					
					if(jobj.getString(CODE_TAG) != null && jobj.getString(CODE_TAG).equals("1") && !jobj.isNull(HOTPOSTS_TAG)) {						
						joArray = jobj.getJSONArray(HOTPOSTS_TAG);
						for(int i=0; i<joArray.length(); i++) {
							jobj2 = joArray.getJSONObject(i);
							floorList = new ArrayList<HashMap<String, Object>>();
							
							if(jobj2!=null){
								int len = jobj2.length();
								if(!jobj2.isNull(NON_TAG)) {
									len = Integer.parseInt(jobj2.getString(NON_TAG));
								}
								for(int j=len-1; j>=0; j--) {
									if(jobj2.has(String.valueOf(j+1))) {
										jobj3 = jobj2.getJSONObject(String.valueOf(j+1));
										try {
											map = new HashMap<String, Object>();
											if(!jobj3.isNull(P_TAG)) {
												map.put(P_TAG, jobj3.getString(P_TAG));
												if(j==len-1 && !jobj2.isNull(NON_TAG)) resultsMap.put(jobj3.getString(P_TAG), jobj2.getString(NON_TAG));
											}				     
											else map.put(P_TAG, "");
											if(!jobj3.isNull(VOTE_TAG))
											     map.put(VOTE_TAG, jobj3.getString(VOTE_TAG));
											else map.put(VOTE_TAG, "");
											if(!jobj3.isNull(USERNAME_TAG))
											     map.put(USERNAME_TAG, Tools.htmlDecoder(jobj3.getString(USERNAME_TAG)));
											else map.put(USERNAME_TAG, "");
											if(!jobj3.isNull(TIME_TAG))
											     map.put(TIME_TAG, jobj3.getString(TIME_TAG));
											else map.put(TIME_TAG, ""); 
											if(!jobj3.isNull(B_TAG))
											     map.put(B_TAG, Tools.htmlDecoder(jobj3.getString(B_TAG)));
											else map.put(B_TAG, "");
											if(!jobj3.isNull(IP_TAG))
											     map.put(IP_TAG, jobj3.getString(IP_TAG));
											else map.put(IP_TAG, "");
											if(!jobj3.isNull(F_TAG))
											     map.put(F_TAG, Tools.htmlDecoder(jobj3.getString(F_TAG)));
											else map.put(F_TAG, "");
											if(!jobj3.isNull(L_TAG))
											     map.put(L_TAG, jobj3.getString(L_TAG));
											else map.put(L_TAG, "");
											if(!jobj3.isNull(BOARDID_TAG))
											     map.put(BOARDID_TAG, jobj3.getString(BOARDID_TAG));
											else map.put(BOARDID_TAG, "");
											floorList.add(map);
										} catch (Exception e) {
											// TODO Auto-generated catch block
											e.printStackTrace();
										}
									}
								}
								list.add(floorList);
							}							
							
						}
						resultsMap.put(HOTPOSTS_TAG, list);
					}
				}
						
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				NeteaseHttpClient.removeCache(mContext, url);
			}
		}
//		resultsMap.put(HOTPOSTS_TAG, new ArrayList<List<HashMap<String, Object>>>());
		return resultsMap;
	}
	
	public static HashMap<String, Object> getLatestComments(
			String boardId, String docId, int start, int end, int hidenCondition, int hiddenStart, int hiddenEnd,Context mContext) {
		HashMap<String, Object> resultsMap = new HashMap<String, Object>();
		List<List<HashMap<String, Object>>> list = new ArrayList<List<HashMap<String, Object>>>();
		List<HashMap<String, Object>> floorList = null; 

		JSONObject jobj = null;
		JSONObject jobj2 = null;
		JSONObject jobj3 = null;
		JSONArray joArray = null;
		HashMap<String, Object> map = null;

		String url = LATEST_COMMENTS_URL + boardId + "/" + 
		     docId + "/desc/" + start + "/" + end + "/" + hidenCondition + "/" + hiddenStart + "/" + hiddenEnd;

		String results = HttpUtils.getResponseFromGetUrl(url, 30000,mContext);

		
		if (results != null && !results.equals("")) {

			try {
				jobj = new JSONObject(results);
				if(jobj != null) {
					
					resultsMap.put(CODE_TAG, jobj.getString(CODE_TAG));
					
					if(jobj.getString(CODE_TAG) != null && jobj.getString(CODE_TAG).equals("1")) {
						resultsMap.put(PRCOUNT_TAG, jobj.getString(PRCOUNT_TAG));
						resultsMap.put(PTCOUNT_TAG, jobj.getString(PTCOUNT_TAG));
						
						if(!jobj.isNull(NEWPOSTS_TAG)) joArray = jobj.getJSONArray(NEWPOSTS_TAG);
						for(int i=0; joArray!=null && i<joArray.length(); i++) {
							jobj2 = joArray.getJSONObject(i);
							floorList = new ArrayList<HashMap<String, Object>>();
							if(jobj2!=null){
								int len = jobj2.length();
								if(!jobj2.isNull(NON_TAG)) {
									//resultsMap.put(String.valueOf(i), jobj2.getString(NON_TAG));
									len = Integer.parseInt(jobj2.getString(NON_TAG));
								}
								for(int j=len-1; j>=0; j--) {
									if(jobj2.has(String.valueOf(j+1))) {
										jobj3 = jobj2.getJSONObject(String.valueOf(j+1));
										try {
											map = new HashMap<String, Object>();
											if(!jobj3.isNull(P_TAG)) {
												map.put(P_TAG, jobj3.getString(P_TAG));
												if(j==len-1 && !jobj2.isNull(NON_TAG)) {
													resultsMap.put(jobj3.getString(P_TAG), jobj2.getString(NON_TAG));
												}
											}											     
											else map.put(P_TAG, "");
											if(!jobj3.isNull(VOTE_TAG))
											     map.put(VOTE_TAG, jobj3.getString(VOTE_TAG));
											else map.put(VOTE_TAG, "");
											if(!jobj3.isNull(USERNAME_TAG))
											     map.put(USERNAME_TAG, Tools.htmlDecoder(jobj3.getString(USERNAME_TAG)));
											else map.put(USERNAME_TAG, "");
											if(!jobj3.isNull(TIME_TAG))
											     map.put(TIME_TAG, jobj3.getString(TIME_TAG));
											else map.put(TIME_TAG, "");
											if(!jobj3.isNull(B_TAG))
											     map.put(B_TAG, Tools.htmlDecoder(jobj3.getString(B_TAG)));
											else map.put(B_TAG, "");
											if(!jobj3.isNull(IP_TAG))
											     map.put(IP_TAG, jobj3.getString(IP_TAG));
											else map.put(IP_TAG, "");
											if(!jobj3.isNull(F_TAG))
											     map.put(F_TAG, Tools.htmlDecoder(jobj3.getString(F_TAG)));
											else map.put(F_TAG, "");
											if(!jobj3.isNull(L_TAG))
											     map.put(L_TAG, jobj3.getString(L_TAG));
											else map.put(L_TAG, "");
											if(!jobj3.isNull(BOARDID_TAG))
											     map.put(BOARDID_TAG, jobj3.getString(BOARDID_TAG));
											else map.put(BOARDID_TAG, "");
											floorList.add(map);
										} catch (Exception e) {
											// TODO Auto-generated catch block
											e.printStackTrace();
										}
									}
								}
								list.add(floorList);
							}							
							
						}						
						resultsMap.put(NEWPOSTS_TAG, list);
					}					
				}								
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				NeteaseHttpClient.removeCache(mContext, url);
			}
		}
//		resultsMap.put(NEWPOSTS_TAG, new ArrayList<List<HashMap<String, Object>>>());
		return resultsMap;
	}
	
	public static HashMap<String, Object> getOnePieceOfComment(
			String boardId, String docId, String p,Context mContext) {
		HashMap<String, Object> resultsMap = new HashMap<String, Object>();
		List<List<HashMap<String, Object>>> list = new ArrayList<List<HashMap<String, Object>>>();
		List<HashMap<String, Object>> floorList = null; 

		JSONObject jobj = null;
		JSONObject jobj2 = null;
		JSONObject jobj3 = null;
		JSONArray joArray = null;
		HashMap<String, Object> map = null;

		String url = ONE_COMMENT_URL + boardId + "/" + docId + "_" + p;
//		System.out.println("url "+url);
		String results = HttpUtils.getResponseFromGetUrl(url, 30000,mContext);
		
		if (results != null && !results.equals("")) {

			try {
				jobj = new JSONObject(results);
				if(jobj != null) {
					resultsMap.put(CODE_TAG, jobj.getString(CODE_TAG));
					if(jobj.getString(CODE_TAG) != null 
							&& (jobj.getString(CODE_TAG).equals("1") || jobj.getString(CODE_TAG).equals("2"))) {
						try {							
							resultsMap.put(VOTE_TAG, jobj.getInt(VOTE_TAG));
							resultsMap.put(USERNAME_TAG, jobj.getString(USERNAME_TAG));
							resultsMap.put(TIME_TAG, jobj.getString(TIME_TAG));
							resultsMap.put(P_TAG, jobj.getString(P_TAG));					
						} catch (Exception e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						if(!jobj.isNull(B_TAG)) joArray = jobj.getJSONArray(B_TAG);
						for(int i=0; joArray!=null && i<joArray.length(); i++) {
							jobj2 = joArray.getJSONObject(i);
							floorList = new ArrayList<HashMap<String, Object>>();
							if(jobj2!=null){
								int len = jobj2.length();
		
								for(int j=len-1; j>=0; j--) {
									if(jobj2.has(String.valueOf(j+1))) {
										jobj3 = jobj2.getJSONObject(String.valueOf(j+1));
										try {
											map = new HashMap<String, Object>();
											if(!jobj3.isNull(VOTE_TAG))
											     map.put(VOTE_TAG, jobj3.getString(VOTE_TAG));
											else map.put(VOTE_TAG, "");
											if(!jobj3.isNull(USERNAME_TAG))
											     map.put(USERNAME_TAG, Tools.htmlDecoder(jobj3.getString(USERNAME_TAG)));
											else map.put(USERNAME_TAG, "");
											if(!jobj3.isNull(TIME_TAG))
											     map.put(TIME_TAG, jobj3.getString(TIME_TAG));
											else map.put(TIME_TAG, "");
											if(!jobj3.isNull(P_TAG))
											     map.put(P_TAG, jobj3.getString(P_TAG));
											else map.put(P_TAG, "");
											if(!jobj3.isNull(B_TAG))
											     map.put(B_TAG, Tools.htmlDecoder(jobj3.getString(B_TAG)));
											else map.put(B_TAG, "");
											if(!jobj3.isNull(IP_TAG))
											     map.put(IP_TAG, jobj3.getString(IP_TAG));
											else map.put(IP_TAG, "");
											if(!jobj3.isNull(F_TAG))
											     map.put(F_TAG, Tools.htmlDecoder(jobj3.getString(F_TAG)));
											else map.put(F_TAG, "");
											if(!jobj3.isNull(PI_TAG))
											     map.put(PI_TAG, jobj3.getString(PI_TAG));
											else map.put(PI_TAG, "");
											if(!jobj3.isNull(BOARDID_TAG))
											     map.put(BOARDID_TAG, jobj3.getString(BOARDID_TAG));
											else map.put(BOARDID_TAG, "");
											floorList.add(map);
										} catch (Exception e) {
											// TODO Auto-generated catch block
											e.printStackTrace();
										}
									}
								}
								list.add(floorList);
							}							
							
						}						
						resultsMap.put(B_TAG, list);
					}
				}				
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				NeteaseHttpClient.removeCache(mContext, url);
			}
		}
		return resultsMap;
	}
	
	public static String postComment(String board, String threadid, String quote, String body, String userid, 
			String nickname, String IP, String hiddename,Context mContext) {
		String results = "";
		String returnCode = "";
		JSONObject jobj = null;
		
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(BOA_TAG, board));
		params.add(new BasicNameValuePair(THREADID_TAG, threadid));
		params.add(new BasicNameValuePair(QUOTE_TAG, quote));
		params.add(new BasicNameValuePair(BODY_TAG, body));
		params.add(new BasicNameValuePair(USERID_TAG, userid));
		params.add(new BasicNameValuePair(NICKNAME_TAG, nickname));
		params.add(new BasicNameValuePair(IP_TAG, IP));
		params.add(new BasicNameValuePair(HIDDENAME_TAG, hiddename));

		results = HttpUtils.getResponseFromPostUrl(params, POST_COMMENTS_URL,mContext);

		if (results != null && !results.equals("")) {
			
				try {
					jobj = new JSONObject(results);
					if(jobj != null && !jobj.isNull(CODE_TAG)) {
						returnCode = jobj.getString(CODE_TAG);
						if(returnCode.equals("1")) return returnCode;
						else return jobj.getString(ERRMSG_TAG);				
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}				
		}

		return returnCode;
		
	}
	
	public static String postComment(String board, String threadid, String body, String userid, 
			String nickname, String IP, String hiddename,Context mContext) {
		
		String results = "";
		String returnCode = "";
		JSONObject jobj = null;
		
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(BOA_TAG, board));
		params.add(new BasicNameValuePair(THREADID_TAG, threadid));
		params.add(new BasicNameValuePair(BODY_TAG, body));
		params.add(new BasicNameValuePair(USERID_TAG, userid));
		params.add(new BasicNameValuePair(NICKNAME_TAG, nickname));
		params.add(new BasicNameValuePair(IP_TAG, IP));
		params.add(new BasicNameValuePair(HIDDENAME_TAG, hiddename));

		results = HttpUtils.getResponseFromPostUrl(params, POST_COMMENTS_URL,mContext);
		
		if (results != null && !results.equals("")) {
			
				try {
					jobj = new JSONObject(results);
					if(jobj != null && !jobj.isNull(CODE_TAG)) {
						returnCode = jobj.getString(CODE_TAG);
						if(returnCode.equals("1")) return returnCode;
						else return jobj.getString(ERRMSG_TAG);
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}				
		}

		return returnCode;
	}
	
	public static List<HashMap<String, Object>> getRankCommentsList(String type,Context mContext) {
		List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();

		JSONArray jsonArray = null;
		JSONObject jobj = null;
		JSONObject jobj2 = null;
		HashMap<String, Object> map = null;

		String url = Rank_Comments_Url + type + ".html";

		String results = HttpUtils.getResponseFromGetUrl(url, 30000,mContext);

		if (results != null && !results.equals("")) {

			try {
				jobj = new JSONObject(results);
				
				if(jobj != null) {
					jsonArray = jobj.getJSONArray(type);

					for (int j = 0; jsonArray != null && j < jsonArray.length(); j++) {
						try {
							jobj2 = jsonArray.getJSONObject(j);
							map = new HashMap<String, Object>();
							map.put(POSTID_TAG, jobj2.getString(POSTID_TAG));
							map.put(TITLE_TAG, Tools.htmlDecoder(jobj2.getString(TITLE_TAG)));
							map.put(REPLYCOUNT_TAG, jobj2.getInt(REPLYCOUNT_TAG));
							map.put(REPLYBOARD_TAG, jobj2.getString(REPLYBOARD_TAG));
							
							list.add(map);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					}
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				NeteaseHttpClient.removeCache(mContext, url);
			}
		}

		return list;
	}
	                                
	public static List<HashMap<String, Object>> getTopCommentsList(
			int start, int len,Context mContext) {
		List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();

		JSONObject jobj = null;
		JSONObject jobj2 = null;
		JSONArray joArray = null;
		HashMap<String, Object> map = null;

		String url = TOP_COMMENTS_URL + start + "-" + len + ".html";
		String results = HttpUtils.getResponseFromGetUrl(url, 30000,mContext);

		if (results != null && !results.equals("")) {

			try {
				jobj = new JSONObject(results);
				if (jobj != null) {
					joArray = jobj.getJSONArray(TIES_TAG);
					for (int i = 0; i < joArray.length(); i++) {
						jobj2 = joArray.getJSONObject(i);
						if (jobj2 != null) {
							try {
								map = new HashMap<String, Object>();
								if (!jobj2.isNull(TITLE_TAG))
									map.put(TITLE_TAG, Tools.htmlDecoder("原文："+jobj2.getString(TITLE_TAG)));
								else
									map.put(TITLE_TAG, "");
								if (!jobj2.isNull(DOCID_TAG))
									map.put(DOCID_TAG, Tools.htmlDecoder(jobj2
											.getString(DOCID_TAG)));
								else
									map.put(DOCID_TAG, "");
								if (!jobj2.isNull(REPLYBOARD_TAG))
									map.put(REPLYBOARD_TAG, Tools
											.htmlDecoder(jobj2
													.getString(REPLYBOARD_TAG)));
								else
									map.put(REPLYBOARD_TAG, "");
								if (!jobj2.isNull(FLOOR_TAG))
									map.put(FLOOR_TAG, Tools.htmlDecoder(jobj2
											.getString(FLOOR_TAG)));
								else
									map.put(FLOOR_TAG, "");
								if (!jobj2.isNull(AUTHOR_TAG))
									map.put(AUTHOR_TAG, Tools.htmlDecoder(jobj2
											.getString(AUTHOR_TAG)));
								else
									map.put(AUTHOR_TAG, "");
								if (!jobj2.isNull(TIE_TAG))
									map.put(TIE_TAG, Tools.htmlDecoder(jobj2
											.getString(TIE_TAG)));
								else
									map.put(TIE_TAG, "");
								if (!jobj2.isNull(PTIME_TAG))
									map.put(PTIME_TAG, Tools.htmlDecoder(jobj2
											.getString(PTIME_TAG)));
								else
									map.put(PTIME_TAG, "");
								
								list.add(map);

							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
				}

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				NeteaseHttpClient.removeCache(mContext, url);
			}
		}
		return list;
	}
	
	public static void upVote(String board, String docid, String postid,Context mContext) {
		String url = "";
		if(postid.contains(docid)) {
			url = Up_Vote_Url + board + "/" + postid;
		} else {
			url = Up_Vote_Url + board + "/" + docid + "_" + postid;
		}
		HttpUtils.getResponseFromPostUrl(null, url,mContext);
	}
	
	/**获取跟贴数*/
	public static int getIamgeCommentNum(String boardid, String postid, Context context){
		int num = 0;
		if("".equals(postid)||"".equals(boardid)){
			return -1;
		}
		try {
			if(null!=boardid && !"".equals(boardid) && null!=postid && !"".equals(postid)){
				String url = IMAGE_COMMENT_NUM_URL + boardid + "/" + postid;
				
				String rst = HttpUtils.getResponseFromGetUrl(url, 5000, context);
				
				JSONObject jobj = Tools.StringToJson(rst);
				if(null!=jobj){
					if(!jobj.isNull("ptcount"))
						num = jobj.getInt("ptcount");
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return num;
	}
}
